/*****************************************************************************/ /*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "common.h"
#include "swap.h"
#include "nor_cmd.h"
#include "nand_w25n.h"
#include "bsp_api.h"
#include "fun_list.h"
#include "mbedtls\aes.h"
#include "../coremark/coremark.h"
#include "mem_alloc.h"

/*****************************************************************************
* @brief   fun.
*****************************************************************************/
void sw_iic_opt(void);
void sys_flash_init(void);
void sys_led_ctr(void);
void nor_flash_demo(void);
void sys_tick_exe(void);
void system_info(void);
void str_demo(void);
void sys_par_load(void);
FUN_LIST_REF(nsys);
FUN_LIST_REF(napp);


/*****************************************************************************
* @brief   var.
*****************************************************************************/


/*****************************************************************************
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(const void *argument)
{
	uint32_t tick;
	uu32 *drm;
	
	bsp_Init();
	sys_init();
	
	app_timer_task_add(sys_tick_exe);	
	
	drm = mem_dram.api->malloc(5*1024);
	
	while (1)
	{
		sub2_add8(2,200);
		
		fun_list_run_once(nsys);
		fun_list_run_once(napp);
		
		//sys_st.info.stack_used = stack_used();

		sys_st.u_tick = app_tick_get();
		set_u16(drm, tick, 5*1024>>1);
		lrc_u16(drm, 5*1024>>1);
		
		// 虚拟RTC
		pulse_creat(&sys_st.pulse, sys_st.u_tick);
		if (sys_st.pulse.pulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
		}

		sys_app_run_once();


		tick = app_tick_get();
		//bsp_dly_ms(200);
		sys_st.bsp_dly_verify = app_tick_get() - tick;
		
		NOP1();
		osDelay(ms_ticks(20));
		NOP1();
	}
}

/*****************************************************************************
* @brief   coremark.
*****************************************************************************/
void coremark_demo(void)
{
MAIN_RETURN_TYPE
core_main(int argc, char *argv[]);
	
		if(sys_st.cmd.coremark_run)
		{
			sys_st.cmd.coremark_run = 0;
			core_main(0,0);
		}
}
DBG_FUN_EXPORT(coremark_demo);
obj_app_order(coremark_demo);

/*****************************************************************************
* @brief   led control.
*****************************************************************************/
void sys_led_ctr(void)
{
	static word_tt st = {1};

	if(!sys_st.pulse.pulse_100ms)
		return;
	
	// 位移
	if (sys_st.pulse.pulse_100ms)
	{
		st.uval = bit16_roll_shift_left(st.uval, 15, 1);
	}

	// 输出
	o_led1.out(st.bits.b0 || st.bits.b2 || st.bits.b8);
}
DBG_FUN_EXPORT(sys_led_ctr);
obj_app_order(sys_led_ctr);

/*****************************************************************************
* @brief   flash opt.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_flash_init(void)
{
	uu32 * ptr;
	uu32 clk_sr, clk_end;
	
	clk_sr = bsp_cpu_clks_get();
	loop(bsp_sdram_size()>>2)
		outp32(0x80000000+index*4, 0x80000000+index*4);
	clk_end = bsp_cpu_clks_get();
	dbg_print("sdram write size : %d MB, time: %d us\n", bsp_sdram_size()>>20, bsp_clks_to_us(clk_end - clk_sr));
	NOP1();	
	
	clk_sr = bsp_cpu_clks_get();
	sys_st.st.common_dbg = 0;
	loop(bsp_sdram_size()>>2)
	{
		if(inp32(0x80000000+index*4) != (0x80000000+index*4))
		{
			sys_st.st.common_dbg++;
		}
	}
	clk_end = bsp_cpu_clks_get();
	dbg_print("sdram compare size : %d MB, time: %d us\n", bsp_sdram_size()>>20, bsp_clks_to_us(clk_end - clk_sr));
	
	dbg_trace_nzero(sys_st.st.common_dbg, "sdram check error \n");

	ptr = mem_dram.api->malloc(4*1024+5);
	set_u32(ptr, 0x123456, 512);
	mem_sram.api->free(ptr);
	
	NOP1();
}
//obj_init_order5(sys_flash_init);

/*****************************************************************************
* @brief   str_demo
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void str_demo(void)
{
	int32_t site;
	e_copy_type cfg;

	uint8_t *buff = malloc(2100);
	uint8_t *pdes = malloc(2100);

	loop(1024)
		buff[index] = index;

	site = search_3B(buff, 512, 0, 511, 0x454647);
	dbg_print("search 0x454647 = %d \n", site);
	site = poll_search_3B(buff, 16, 8, 7, 0x0F0001);
	dbg_print("search 0x800001 = %d \n", site);
	site = poll_search_4B(buff, 16, 8, 7, 0x0F000102);
	dbg_print("search 0x80000102 = %d \n", site);

	cfg.des_len = 0x40;
	cfg.src_len = 0x20;
	cfg.start = 0x10;
	cfg.end = 0x0F;
	cfg.edat = 0x1F000102;

	ecopy(&cfg, buff, pdes, 1);
	dbg_print("copy e 1B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 2);
	dbg_print("copy e 2B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 3);
	dbg_print("copy e 3B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 4);
	dbg_print("copy e 4B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);

	cfg.des_len = 0x8;
	ecopy(&cfg, buff, pdes, 1);
	dbg_print("copy e 1B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 2);
	dbg_print("copy e 2B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 3);
	dbg_print("copy e 3B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 4);
	dbg_print("copy e 4B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);

	cfg.des_len = 0x40;
	cfg.edat = 0x0E0F1011;

	ecopy(&cfg, buff, pdes, 1);
	dbg_print("copy e 1B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 2);
	dbg_print("copy e 2B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 3);
	dbg_print("copy e 3B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);
	ecopy(&cfg, buff, pdes, 4);
	dbg_print("copy e 4B : site =%d, len = %d \n", cfg.res_site, cfg.res_len);
	dbg_u8_print("res : ", pdes, 32, 40);

	cfg.des_len = 0x40;
	cfg.edat = 0x1C1D1E1F;
	ecopy(&cfg, buff, pdes, 4);
	dbg_print("copy e 4B : site =%d, len = %d, next =%d \n", cfg.res_site, cfg.res_len, cfg.res_next);
	dbg_u8_print("res : ", pdes, 32, 40);

	cfg.des_len = 0x40;
	cfg.edat = 0x04050607;
	ecopy(&cfg, buff, pdes, 4);
	dbg_print("copy e 4B : site =%d, len = %d, next =%d \n", cfg.res_site, cfg.res_len, cfg.res_next);
	dbg_u8_print("res : ", pdes, 32, 40);

	free(buff);
	free(pdes);
}

/*****************************************************************************
* @brief   iic opt.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sw_iic_opt(void)
{
}

/*****************************************************************************
*
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{
}
DBG_FUN_EXPORT(task_sw_hook);

/*****************************************************************************
*
* @brief   sys_par_load
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_par_load(void)
{

}

/*****************************************************************************
*
* @brief   sys_tick_exe
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_tick_exe(void)
{
}

/*****************************************************************************
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(void)
{
	dbg_puts("===========================================================\n");
	dbg_print("CPU = %d MHz, AHB = %d MHz, OSC = %d MHz\n",
			  bsp_info.clk.CPU / 1000000,
			  bsp_info.clk.AHB / 1000000,
			  bsp_info.clk.OSC / 1000000);
	dbg_puts("===========================================================\n");
}
obj_init_order3(system_info);

/*****************************************************************************
* @brief   tick get.
*
* @ Pass/ Fail criteria: tick
*****************************************************************************/
unsigned long rt_tick_get(void)
{
	return app_tick_get();
}

fun_list_hook(nsys)
{

}